package xin.marcher.module.construction.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import xin.marcher.module.common.constants.construction.ProductConstants;
import xin.marcher.module.common.enums.construction.AuditorRoleEnum;
import xin.marcher.module.common.enums.inventory.InventoryExceptionCode;
import xin.marcher.module.common.exception.BaseBizException;
import xin.marcher.module.construction.domain.dto.AuditorListConfigDTO;
import xin.marcher.module.construction.domain.dto.AuditorListConfigResultDTO;
import xin.marcher.module.construction.domain.request.AuditorListConfigRequest;
import xin.marcher.module.construction.repository.ProductAuditRepository;
import xin.marcher.module.construction.service.AuditorListConfigService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

public class AuditorListConfigServiceImpl implements AuditorListConfigService {

    @Resource
    private ProductAuditRepository productAuditRepository;

    /**
     * 新增审核人员配置
     *
     * @param request
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AuditorListConfigResultDTO saveAuditorConfig(AuditorListConfigRequest request) {
        // 参数校验
        checkSaveParams(request);
        productAuditRepository.saveAuditorConfig(request);
        return new AuditorListConfigResultDTO(Boolean.TRUE);
    }

    /**
     * 修改审核人员配置
     *
     * @param request
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AuditorListConfigResultDTO updateAuditorConfig(AuditorListConfigRequest request) {
        // 参数校验
        checkUpdateParams(request);
        productAuditRepository.updateAuditorConfig(request);
        return new AuditorListConfigResultDTO(Boolean.TRUE);
    }

    /**
     * 删除审核人员配置
     *
     * @param auditorIdList
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AuditorListConfigResultDTO deleteAuditorConfig(List<Integer> auditorIdList) {
        productAuditRepository.deleteAuditorConfig(auditorIdList);
        return new AuditorListConfigResultDTO(Boolean.TRUE);
    }

    /**
     * 查询审核人员配置
     *
     * @param auditorIdList
     * @return
     */
    @Override
    public List<AuditorListConfigDTO> queryAuditorConfig(List<Integer> auditorIdList) {
        // 参数校验
        checkQueryParams(auditorIdList);
        return productAuditRepository.queryAuditorConfig(auditorIdList);
    }


    /**
     * 新增审核人员入参校验
     *
     * @param request
     */
    private void checkSaveParams(AuditorListConfigRequest request) {
        if (Objects.isNull(request)) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (Objects.isNull(request.getAuditorId())) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (StringUtils.isEmpty(request.getAuditorName())) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (Objects.isNull(AuditorRoleEnum.getByCode(request.getAuditorRole()))) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }
    }

    /**
     * 修改审核人员入参校验
     *
     * @param request
     */
    private void checkUpdateParams(AuditorListConfigRequest request) {
        if (Objects.isNull(request)) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (Objects.isNull(request.getAuditorId())) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (StringUtils.isEmpty(request.getAuditorName()) && Objects.isNull(AuditorRoleEnum.getByCode(request.getAuditorRole()))) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }
    }

    /**
     * 查询审核人员入参校验
     *
     * @param auditorIdList
     */
    private void checkQueryParams(List<Integer> auditorIdList) {
        if (CollectionUtils.isEmpty(auditorIdList)) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (auditorIdList.size() > ProductConstants.QUERY_ITEM_MAX_COUNT) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }
    }
}
